/**
 * Created by zzy on 2017/1/13.
 */
var $ = require('jquery');
require('layout/util');
var $alert = require('libs/alert');
var $form = require('js-path/formValide.js');
var $form2 = require('js-path/formValideNotSub.js');
var col = 5;
var mms = 3;
$(function(){
    pageInit();
});
function pageInit() {
    var id = $.getUrlParam('id');
    loadAndInit(id);
}
function loadAndInit(id) {
    $.ajax({
        url:'api/v1/p/mms/authRole/load',
        type:'post',
        data:{
            id:id
        },
        dataType:"json",
        success:function (result) {
            if(result.code==200){
                var data = result.data;
                dataInit(data);
            }else{
                $alert(result.message);
            }
        }
    })
}
function dataInit(data) {
    $("#id").val(data.id);
    $("#role_name").val(data.roleName);
    $("#role_desc").val(data.roleDesc);
    $("#issys").val(data.issys);
    $.each($("#main_form input[name=systemId]"),function (k,o) {
        if($(o).val()==data.systemId){
            $("#main_form input[name=systemId]").eq(k).attr("checked","checked");
            return;
        }
    });
    $.ajax({
        url:"api/v1/p/mms/authAuthority/queryAuthority",
        type:"post",
        data:{
            systemId:data.systemId
        },
        dataType:"json",
        success:function (result) {
            if(result.code==200){
                authorityInit(result.data,data.roleAuthorityDos);
            }else{
                $alert(result.message);
            }
        }
    })
}
function authorityInit(data,defaultCheckedValue) {
    var authorityIds = new Array();
    if(defaultCheckedValue!=undefined){
        $.each(defaultCheckedValue,function (k,o) {
            authorityIds.push(o.authorityId);
        })
    }
    $("#div_authority").html("");
    var len = data.length;
    if(len!=0){
        var divArray = new Array(Math.floor((len-1)/col)+1);
        $.each(divArray,function (k,o) {
            divArray[k] = $("<div style='padding-left: 15px'></div>");
        });
        $.each(data,function (k,o) {
            if($.inArray(data[k].id, authorityIds)==-1){
                divArray[Math.floor(k/col)].append("<input type='checkbox' name='authorityId' value='"+data[k].id+"' disabled><span style='margin-right: 10px'>"+data[k].name+"</span>");
            }else{
                divArray[Math.floor(k/col)].append("<input type='checkbox' name='authorityId' value='"+data[k].id+"' disabled checked><span style='margin-right: 10px'>"+data[k].name+"</span>");
            }
        });
        $.each(divArray,function (k,o) {
            $("#div_authority").append(divArray[k]);
        });
    }
}

